-------------------------------------------------------------
--- Lazy nvim plugin manager
------------------------------------------------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"https://github.com/folke/lazy.nvim.git",
		"--branch=stable", -- latest stable release
		lazypath,
	})
end
vim.opt.rtp:prepend(lazypath)
--------------------------------------------------------------
--- Install plugins
--------------------------------------------------------------
--------------------------------------------------------------
--- LuaLines,
--- nvim-tree.lua,
--- telescope
--- bufferline
--------------------------------------------------------------
require("lazy").setup({
	"nvim-lualine/lualine.nvim",
	dependencies = { "nvim-tree/nvim-web-devicons" },
	{
		"nvim-tree/nvim-tree.lua",
		version = "*",
		lazy = false,
		dependencies = {
			"nvim-tree/nvim-web-devicons",
		},
		config = function()
			require("nvim-tree").setup({})
		end,
	},
	{
		"nvim-telescope/telescope.nvim",
		tag = "0.1.8",
		dependencies = { "nvim-lua/plenary.nvim" },
	},
	{ "akinsho/bufferline.nvim", version = "*", dependencies = "nvim-tree/nvim-web-devicons" },
	{ "majutsushi/tagbar" },
	{
		"nvim-telescope/telescope-fzf-native.nvim",
		build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build",
	},
	{ "nvim-treesitter/nvim-treesitter", build = ":TSUpdate" },
	{ "RRethy/base16-nvim" },
	{ "natecraddock/workspaces.nvim" },
	{ "stevearc/conform.nvim", opts = {} },
})

------------------------------------------------------------------------------------------
--- buffer line configuration
------------------------------------------------------------------------------------------
require("bufferline").setup({})
------------------------------------------------------------------------------------------
--- telescope
------------------------------------------------------------------------------------------
require("telescope.builtin")
require("telescope").load_extension("workspaces")
------------------------------------------------------------------------------------------
--- lualine configuration
------------------------------------------------------------------------------------------
require("lualine").setup({
	options = {
		icons_enabled = true,
		theme = "auto",
		component_separators = { left = "", right = "" },
		section_separators = { left = "", right = "" },
		disabled_filetypes = {
			statusline = {},
			winbar = {},
		},
		ignore_focus = {},
		always_divide_middle = true,
		globalstatus = false,
		refresh = {
			statusline = 1000,
			tabline = 1000,
			winbar = 1000,
		},
	},
	sections = {
		lualine_a = { "mode" },
		lualine_b = { "branch", "diff", "diagnostics" },
		lualine_c = { "filename" },
		lualine_x = { "encoding", "fileformat", "filetype" },
		lualine_y = { "progress" },
		lualine_z = { "location" },
	},
	inactive_sections = {
		lualine_a = {},
		lualine_b = {},
		lualine_c = { "filename" },
		lualine_x = { "location" },
		lualine_y = {},
		lualine_z = {},
	},
	tabline = {},
	winbar = {},
	inactive_winbar = {},
	extensions = {},
})
-------------------------------------------
--- Base 16 color sheme
-------------------------------------------
vim.cmd("colorscheme base16-ocean")
-------------------------------------------
--- workspace configuration
-------------------------------------------

local util = require("workspaces.util")
require("workspaces").setup({
	-- path to a file to store workspaces data in
	-- on a unix system this would be ~/.local/share/nvim/workspaces
	path = vim.fn.stdpath("data") .. util.path.sep .. "workspaces",

	-- to change directory for all of nvim (:cd) or only for the current
	-- window (:lcd). if you are unsure, you likely want this to be true.
	-- global_cd = true,

	-- controls how the directory is changed. valid options are "global", "local", and "tab"
	--   "global" changes directory for the neovim process. same as the :cd command
	--   "local" changes directory for the current window. same as the :lcd command
	--   "tab" changes directory for the current tab. same as the :tcd command
	--
	-- if set, overrides the value of global_cd
	cd_type = "global",

	-- sort the list of workspaces by name after loading from the
	-- workspaces path.
	sort = true,

	-- sort by recent use rather than by name. requires sort to be true
	mru_sort = true,

	-- option to automatically activate workspace when opening neovim in a workspace directory
	auto_open = false,

	-- enable info-level notifications after adding or removing a
	-- workspace. disable to only be notified for warnings or errors.
	notify_info = true,

	-- lists of hooks to run after specific actions
	-- hooks can be a lua function or a vim command (string)
	-- if only one hook is needed, the list may be omitted
	hooks = {
		add = {},
		remove = {},
		open_pre = {}, -- run before changing directory
		open = {}, -- run after changing directory
	},
})

require("conform").setup({
	formatters_by_ft = {
		lua = { "stylua" },
		-- Conform will run multiple formatters sequentially
		python = { "autopep8" },
		-- Use a sub-list to run only the first available formatter
		javascript = { { "prettier" } },
		-- D language format
		d = { "dfmt" },
	},
})

vim.api.nvim_create_autocmd("BufWritePre", {
	pattern = "*",
	callback = function(args)
		require("conform").format({ bufnr = args.buf })
	end,
})
